package com.ca.project.Server.Modules.Tweets.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ca.project.Server.Modules.Tweets.Context.CheckTweetsContext;
import com.ca.project.Server.Modules.Tweets.Context.CreateTweetsContext;
import com.ca.project.Server.Modules.Tweets.Context.UpdateTweetsContext;
import com.ca.project.Server.Modules.Tweets.Converter.TweetsConverter;
import com.ca.project.Server.Modules.Tweets.Entity.Tweets;
import com.ca.project.Server.Modules.Tweets.PO.CheckTweetsPO;
import com.ca.project.Server.Modules.Tweets.PO.CreateTweetsPO;
import com.ca.project.Server.Modules.Tweets.PO.UpdateTweetsPO;
import com.ca.project.Server.Modules.Tweets.Service.TweetsService;
import com.ca.project.Server.Modules.Tweets.VO.DeleteTweetsVO;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.Tweets.Controller
 * @NAME: TweetsController
 * @USER: ASUS
 * @DATE: 31/12/2024
 * @TIME: 下午3:11
 **/
@RestController
@RequestMapping("/tweets")
@Api(tags = "推文模块")
public class TweetsController {

    @Autowired
    private TweetsConverter tweetsConverter;
    @Autowired
    private TweetsService tweetsService;

    @ApiOperation(
            value = "创建推文接口",
            notes = "该接口提供了创建推文信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/create_tweets")
    public R CreateTweets(@Validated CreateTweetsPO createTweetsPO) {
        CreateTweetsContext createTweetsContext =
                tweetsConverter.CreateTweetsPO2CreateTweetsContext(createTweetsPO);
        tweetsService.createTweets(createTweetsContext);
        return R.success();
    }

    @ApiOperation(
            value = "修改推文接口",
            notes = "该接口提供了修改推文信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/update_tweets")
    public R UpdateTweets(@Validated @RequestBody UpdateTweetsPO updateTweetsPO) {
        UpdateTweetsContext updateTweetsContext =
                tweetsConverter.UpdateTweetsPO2UpdateTweetsContext(updateTweetsPO);
        tweetsService.updateTweets(updateTweetsContext);
        return R.success();
    }

    @ApiOperation(
            value = "查看推文接口",
            notes = "该接口提供了查看推文信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/check_tweets")
    public R CheckTweets(@Validated @RequestBody CheckTweetsPO checkTweetsPO) {
        CheckTweetsContext checkTweetsContext =
                tweetsConverter.CheckTweetsPO2CheckTweetsContext(checkTweetsPO);
        return R.data(tweetsService.checkTweets(checkTweetsContext));
    }

    @ApiOperation(
            value = "删除推文接口",
            notes = "该接口提供了删除推文信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/delete_tweets")
    public R deleteTweets(@Validated @RequestBody DeleteTweetsVO deleteTweetsVO) {
        Long id = deleteTweetsVO.getId();
        String tTitle = deleteTweetsVO.getT_title();
        tweetsService.deleteById(id,tTitle);
        return R.success();
    }

    @ApiOperation(
            value = "查询所有推文信息-分页",
            notes = "该接口提供了查询所有轮播图信息-分页的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/{currentPage}/{PageSize}")
    public R GetTweets(@PathVariable Integer currentPage, @PathVariable Integer PageSize, Tweets tweets) {
        IPage<Tweets> page = tweetsService.getPage(currentPage, PageSize, tweets);
        //如果当前页码之大于总页码值，重新执行查询操作，使用最大页码作为当前页码值
        if (currentPage > page.getPages())//如果当前页码之大于总页码值
            page = tweetsService.getPage((int) page.getPages(), PageSize);
        return R.data(page);
    }


}
